﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace FrogEngine
{
	public class FireParticleEmitter : ParticleEmitter<FireParticleSystem>
	{
		public FireParticleEmitter(IEngine engine)
			: base(engine)
		{
			var updateService = engine.GetService<UpdateEntityService>();
			this.Services.Add(updateService);
			updateService.Subscribe(this);

			this.Position = Vector3.Zero;
			this.random = new Random(123);
		}

		private Random random;
		public override void Update(GameTime gameTime)
		{
			const int fireParticlesPerFrame = 50;
			World = Matrix.CreateTranslation(this.Position);

			// Create a number of fire particles, randomly positioned around a circle.
			for (int i = 0; i < fireParticlesPerFrame; i++)
				this.system.AddParticle(this.Position + RandomPointOnCircle(), Vector3.Up);

			base.Update(gameTime);
		}

		private Vector3 RandomPointOnCircle()
		{
			const float radius = 300;
			const float height = 400;

			double angle = random.NextDouble() * Math.PI * 2;

			float x = (float)Math.Cos(angle);
			float y = (float)Math.Sin(angle);

			return new Vector3(x * radius, y * radius + height, 0);
		}
	}
}
